#include"Sock.hpp"
#include<unistd.h>

void Usage(std::string proc)
{
  cout << "Usage\n\t" << proc <<" server_ip " << " server_port" << endl;
  exit(1);
}
int main(int argc,char* argv[])
{
  if(argc != 3)
  {
    Usage(argv[0]);
  }
  int sock = Sock::Socket();
  uint16_t server_port = atoi(argv[2]);
  string server_ip = argv[1];
  Sock::Connect(sock,server_ip,server_port);
  for(;;)
  {
    char buf[1024] = {0};
    cout << "Please Enter: ";
    fflush(stdout); //上一句话没有\n 所以要显示的话就要刷新缓冲器
    ssize_t n = read(0,buf,sizeof(buf)-1); //注意:从键盘当中读取会读到\n
    buf[n - 1] = '\0';//\n位置->\0  
    
    write(sock,buf,sizeof(buf)-1); //不写入\0,因为\0只是C语言的字符串结束标识符
    
    n = read(sock,buf,sizeof(buf)-1);
    buf[n] = '\0';
    cout <<"server# "<<buf << endl;
  }
  return 0;
}
